/*
 * ObjectChannelWriter.java    0.0.1    27/set/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.net.cs.server;

import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;

import org.mentalsmash.tazio.net.cs.protocol.NIOObjectContainer;
import org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade;
import org.mentalsmash.tazio.net.cs.server.impl.OutputQueue;

/**
 * Abstracts the byte oriented {@link ChannelFacade}'s 
 * {@link OutputQueue} and gives a convenience method to write any serialisable
 * object to that channel.
 * <p>
 * Thought to be used in combination wit the {@link ObjectMessageProtocolHandler}, that in turn
 * notifies applications of incoming Serializable objects.
 * </p>
 * @version    0.0.1    27/set/2009    
 * @author     Andrea Reale <andrea.reale@gmail.com>
 * @see        ObjectMessageProtocolHandler
 */
public class ObjectChannelWriter {

    private ChannelFacade destination;

    /**
     * Create a new <tt>ObjectChannelWriter</tt> wrapping the given
     * <code>destionation</code>
     * @param destination this channel where this instance will write
     *                    objects
     */
    public ObjectChannelWriter(ChannelFacade destination) {
        this.destination = destination;
    }

    /**
     * Returns the current {@link ChannelFacade} this instance is wrapping
     * @return the current {@link ChannelFacade} this instance is wrapping
     */
    public ChannelFacade getDestination() {
        return destination;
    }
   
    /**
     * Sets a destination for this instance to write to
     * @param destination the destination for this instance to write objects to
     */
    public void setDestination(ChannelFacade destination) {
        this.destination = destination;
    }

    /**
     * Writes the given object to the channel the lies under this <tt>ObjcetChannelWriter</tt>.
     * <p>
     * This implementation wraps the <code>msgToSend</code> into an {@link NIOObjectContainer}
     * wrapper and sends it through the channel 
     * </p>
     * @param msgToSend a serializable object to send on the channel
     * @throws IOException if the serialization process fails
     */
    public void writeObjec(Serializable msgToSend) throws IOException {
      ByteBuffer toWrite;
      //wrap the message in the container to allow universal serialisation
      NIOObjectContainer container = new NIOObjectContainer(msgToSend);
      toWrite = ByteBuffer.wrap(container.writeContainer());
      
      destination.getOutputQueue().enqueue(toWrite);

  }
    
    

}
